package se.sll.itintegration.catalogue.gui.service.view;

import java.security.Principal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.SecurityContext;

import org.apache.camel.Exchange;
import org.apache.camel.Property;

import se.sll.itintegration.catalogue.gui.data.ViewBase;
import se.sll.itintegration.catalogue.gui.entity.UserEntity;

/**
 *
 * @author Christer Palm, Nogui
 */
public class ViewBaseProcessor {

    private static final Map<String, String> ROLE_DISPLAY_NAMES = new LinkedHashMap<String, String>() {
        private static final long serialVersionUID = 1L;
        {
            put(UserEntity.ROLES.SUPERADMIN.name(), "Huvudadministratör");
            put(UserEntity.ROLES.CAPACITYADMIN.name(), "Rapportör");
//            put(UserEntity.ROLES.CAREUNITADMIN.name(), "Enhetsadministratör");
        }
    };

    /**
     *
     * @param exchange
     * @param viewBase
     * @param sc
     */
    public void init(Exchange exchange, @Property("view") ViewBase viewBase, @Property("sc") SecurityContext sc) {
        // User name
        Principal principal = sc.getUserPrincipal();
        if (principal != null) {
            String name = principal.getName();
            viewBase.setUsername(name);
        }

        // User roles
        List<String> roles = new ArrayList<>(ROLE_DISPLAY_NAMES.size());
        for (String role : ROLE_DISPLAY_NAMES.keySet()) {
            if (sc.isUserInRole(role)) {
                roles.add(role);
            }
        }
        viewBase.setUserRoles(roles);

        // All roles
        viewBase.setRoles(ROLE_DISPLAY_NAMES);
    }
}
